<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" :width="adaptiveWidth" @ok="handleSubmit" :showFooter="showFooter" destroyOnClose>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { defineComponent, ref, computed, unref, useAttrs } from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from './user.data';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { saveOrUpdateUser, getRoleByUserId,getPostByUserId, } from './user.api';
import { useDrawerAdaptiveWidth } from '/@/hooks/jeecg/useAdaptiveWidth';
import { getTenantId } from '/@/common/util/auth';

// 声明Emits
const emit = defineEmits(['success', 'register']);
const attrs = useAttrs();
const isUpdate = ref(true);
const rowId = ref('');
const departOptions = ref([]);
let isFormDepartUser = false;
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 90,
  schemas: formSchema,
  showActionButtonGroup: false,
});
const showFooter = ref(true);
//表单赋值
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  await resetFields();
  showFooter.value = data?.showFooter ?? true;
  setDrawerProps({ confirmLoading: false, showFooter: showFooter.value });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {
    rowId.value = data.record.id;
    try {
      //角色下拉框处理
      const userRoles = await getRoleByUserId({ userid: data.record.id });
      if (userRoles && userRoles.length > 0) {
        data.record.selectedroles = userRoles;
      }
      //职务下拉框处理
      const postIds = await getPostByUserId({ userid: data.record.id });
      if (postIds && postIds.length > 0) {
        data.record.post = postIds;
      }
    } catch (error) { }
  }
  //处理角色用户列表情况(和角色列表有关系)
  data.selectedroles && (await setFieldsValue({ selectedroles: data.selectedroles }));
  isFormDepartUser = data?.departDisabled === true ? true : false;
  //编辑时隐藏密码/角色列表隐藏角色信息/我的部门时隐藏所属部门
  updateSchema([
    {
      field: 'password',
      ifShow: !unref(isUpdate),
    },
    {
      field: 'confirmPassword',
      ifShow: !unref(isUpdate),
    },
    {
      field: 'selectedroles',
      show: !data.isRole,
    },
  ]);
  // 无论新增还是编辑，都可以设置表单值
  if (typeof data.record === 'object') {
    setFieldsValue({
      ...data.record,
    });
  }
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !showFooter.value });
});


//获取标题
const getTitle = computed(() => {
  if (!unref(isUpdate)) {
    return '新增用户';
  } else {
    return unref(showFooter) ? '编辑用户' : '用户详情';
  }
});
const { adaptiveWidth } = useDrawerAdaptiveWidth();

//提交事件
async function handleSubmit() {
  try {
    let values = await validate();
    setDrawerProps({ confirmLoading: true });
    values.userIdentity === 1 && (values.deptIds = '');
    let isUpdateVal = unref(isUpdate);
    let params = values;
    if (isFormDepartUser) {
      params = { ...params, updateFromPage: 'deptUsers' };
    }
    //提交表单
    await saveOrUpdateUser(params, isUpdateVal);
    //关闭弹窗
    closeDrawer();
    //刷新列表
    emit('success', { isUpdateVal, values });
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
